<template>
  <div>
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :md="6" :lg="4">
          <p>按机构类型选择</p>
          <el-select v-model="Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.DelegationTypeCode" size="mini" placeholder="全部" filterable clearable @change="getInstitutionData()">
            <el-option label="全部" value="" />
            <el-option v-for="item in dict.delegationType" :key="item.code" :label="item.value" :value="item.code" />
          </el-select>
        </el-col>
        <!-- 机构名称 -->
        <el-col :md="6" :lg="4">
          <p>机构名称</p>
          <el-input v-model="Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.Filter" placeholder="请输入" size="mini" clearable @change="getInstitutionData()" />
        </el-col>
        <el-col :md="8" :lg="6">
          <!-- 查询按钮 -->
          <el-button style="margin-top:27px" type="primary" size="mini" @click="getInstitutionData()">查询</el-button>
          <el-button :loading="buLoading" style="margin-top:27px" type="primary" size="mini" @click="exportPhone()">导出机构通讯录</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table v-loading="loading" :data="tableData" stripe size="mini">
      <el-table-column type="index" width="50" align="center" />
      <el-table-column prop="imagePath" label="头像" width="80" align="center">
        <template slot-scope="{row}">
          <img v-if="row.imagePath" width="40" :src="getStaticFileUrl(row.imagePath)">
          <img v-else width="40" src="@/assets/images/person_default.png">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="机构名称" />
      <el-table-column prop="address" label="机构所在地" />
      <el-table-column prop="delegationTypeDes" label="机构类型" width="90" align="center" />
      <el-table-column prop="phoneNumber" label="机构联系方式" width="100" align="center" />
      <el-table-column prop="personCount" label="参赛人数" width="100" align="center" />
      <el-table-column prop="entryCount" label="参赛人次" width="100" align="center" />
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" @click="detail(row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="gl_pt_20" style="text-align: center;">
      <el-pagination
        :current-page="Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.Page"
        :page-size="Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.MaxResultCount"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 50, 100]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>

    <Detail ref="detail" />
  </div>
</template>
<script>
import { api_GET_DownLoadFileAddHeader } from '@/extend/api/file';
import { errorText } from '@/lib';
import { mapState, mapActions } from 'vuex';
import {
  api_GET_EventsManagementCompetitionEntryDataCompDelegationData // 获取机构详细
} from '@/extend/api/gameswaggerApi/app.js';
import {
  Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO
} from '@/lib/form/game/swaggerForm';
import Detail from './detail';
export default {
  name: 'GameDetail',
  components: {
    Detail
  },
  data() {
    return {
      buLoading: false,
      Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO: new Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO(),
      loading: false,
      totalPage: 0, // 分页总条目数
      delegation: {},
      tableData: []
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getDict();
    this.getInstitutionData();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    // 获取表格数据
    getInstitutionData(page) {
      this.loading = true;
      this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.Page = page || 1;
      this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.SkipCount = (this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.Page - 1) * this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.MaxResultCount;
      this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.CompetitionId = this.$route.params.id;
      api_GET_EventsManagementCompetitionEntryDataCompDelegationData(this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO).then(res => {
        this.tableData = res.items;
        this.totalPage = res.totalCount;
        this.loading = false;
      }, () => {
        this.loading = false;
        this.$message({ type: 'error', message: '获取机构明细失败！' });
      });
    },
    // 导出机构通讯录
    exportPhone() {
      var that = this;
      this.buLoading = true;
      const tempUrl = `/api/excel`;
      const fileName = '机构通讯录' + '.xlsx';
      const params = {};
      params.ReportName = 'ComDelegationExcel';
      params.CompetitionId = this.$route.params.id;
      api_GET_DownLoadFileAddHeader(tempUrl, fileName, params).then(res => {
        that.buLoading = false;
      }).catch(error => {
        that.buLoading = false;
        errorText(error).then(function(text) {
          that.$message({ type: 'error', message: text });
        });
      });
    },
    // 查看详情
    detail(row) {
      this.delegation = {
        id: row.delegationId,
        name: row.name
      };
      this.$refs.detail.open(this.delegation);
    },
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    // 分页
    sizeChange: function(MaxResultCount) {
      this.Api_GET_EventsManagementCompetitionEntryDataCompDelegationDataQO.MaxResultCount = MaxResultCount;
      this.getInstitutionData();
    },
    pageIndexChange(currentPage) {
      this.getInstitutionData(currentPage);
    }
  }
};
</script>
